import axios from 'axios';

var commonOper = {
  showWarnAlert: function (params) {
    params.this.$confirm(params.message, params.title, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          params.successFunc && params.successFunc.call();
        }).catch(() => {
          params.failFunc && params.failFunc.call();     
        });
  },
  /* ---cjw add at  03/14/2019 ↓--- */
  showSuccessAlert: function (params) {
    params.this.$success({
      title: params.title || '提示',
      content: params.message,
      okType: params.okType || 'primary',
      onOk() {
        params.successFunc && params.successFunc.call();
      },
      onCancel() { params.failFunc && params.failFunc.call(); },
    });
  },
  showErrorAlert: function (params) {
    params.this.$error({
      title: params.title || '提示',
      content: params.message,
      okType: params.okType || 'primary',
      onOk() {
        params.successFunc && params.successFunc.call();
      },
      onCancel() { params.failFunc && params.failFunc.call(); },
    });
  },
  showWarningAlert: function (params) {
    params.this.$warning({
      title: params.title || '提示',
      content: params.message,
      okType: params.okType || 'primary',
      onOk() {
        params.successFunc && params.successFunc.call();
      },
      onCancel() { params.failFunc && params.failFunc.call(); },
    });
  },
  /* ---cjw add at  03/14/2019 ↑--- */
  /** 显示系统通知 */
  Notification: function (params) {
    params.this.$notification[params.type]({
      message: params.title,
      description: params.message,
    });
  },
  /**发送http post请求 */
  HttpPost: function (params) {

    axios.post(params.url, params.data, {
      headers: params.headers,
    })
      .then(function (response) {
        params.successFunc && params.successFunc.call(params.this, response);
      })
      .catch(function (response) {
        params.failFunc && params.failFunc.call(this, response);
      });

  },
  /**发送http get请求 */
  HttpGet: function (params) {

    axios.get(params.url, params.data)
      .then(function (response) {
        params.successFunc && params.successFunc.call(params.this, response);
      })
      .catch(function (response) {
        params.failFunc && params.failFunc.call(this, response);
      });
  },
  /**发送http put请求 */
  HttpPut: function (params) {

    axios.put(params.url, params.data, {
      headers: params.headers,
    })
      .then(function (response) {
        params.successFunc && params.successFunc.call(params.this, response);
      })
      .catch(function (response) {
        params.failFunc && params.failFunc.call(this, response);
      });

  },
  /**发送http delete请求 */
  HttpDelete: function (params) {

    axios.delete(params.url, params.data)
      .then(function (response) {
        params.successFunc && params.successFunc.call(params.this, response);
      })
      .catch(function (response) {
        params.failFunc && params.failFunc.call(this, response);
      });

  },
  initWebSocket: function (url) {
    return new WebSocket(url);
  },
};
export default commonOper;